package com.example.hao.day10_10;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class MainActivity extends AppCompatActivity {

    private TextView tv_show;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //注册EventBus
        EventBus.getDefault().register(this);
        setContentView(R.layout.activity_main);
        initView();

    }

    private void initView() {

        tv_show = findViewById(R.id.tv_show);

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void abc(Text text) {
        tv_show.setText(text.getText());
        tv_show.setTextColor(text.getColor());
        tv_show.setTextSize(text.getTextSize());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //注销 EventBus
        EventBus.getDefault().unregister(this);
    }

    public void textDownLoad(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
//                tv_show.setText("hello");
//                tv_show.setTextColor(Color.RED);
//                tv_show.setTextSize(30);

                EventBus.getDefault().post(new Text("Hello", Color.RED, 30));
            }
        }).start();
    }

    class Text {
        private String text;
        private int color;
        private int textSize;

        @Override
        public String toString() {
            return "Text{" +
                    "text='" + text + '\'' +
                    ", color=" + color +
                    ", textSize=" + textSize +
                    '}';
        }

        public Text(String text, int color, int textSize) {
            this.text = text;
            this.color = color;
            this.textSize = textSize;
        }

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }

        public int getColor() {
            return color;
        }

        public void setColor(int color) {
            this.color = color;
        }

        public int getTextSize() {
            return textSize;
        }

        public void setTextSize(int textSize) {
            this.textSize = textSize;
        }
    }
}
